package edu.uoc.tdp.pac4.server;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import edu.uoc.tdp.pac4.beans.Attendance;
import edu.uoc.tdp.pac4.beans.Aula;
import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.beans.Group;
import edu.uoc.tdp.pac4.beans.Registration;
import edu.uoc.tdp.pac4.beans.Shift;
import edu.uoc.tdp.pac4.beans.Usuari;
import edu.uoc.tdp.pac4.common.AcademicManagementInterface;
import edu.uoc.tdp.pac4.database.AttendanceManager;
import edu.uoc.tdp.pac4.database.CourseManager;
import edu.uoc.tdp.pac4.database.GroupManager;
import edu.uoc.tdp.pac4.database.RegistrationManager;
import edu.uoc.tdp.pac4.database.RoomManager;
import edu.uoc.tdp.pac4.database.UserManager;

public class AcademicManagementImpl extends java.rmi.server.UnicastRemoteObject
		implements AcademicManagementInterface, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AcademicManagementImpl() throws RemoteException {
		super();
	}

	protected AcademicManagementImpl(int port) throws RemoteException {
		super(port);
	}

	GroupManager groupManager = new GroupManager();
	RegistrationManager registrationManager = new RegistrationManager();
	AttendanceManager attendanceManager = new AttendanceManager();
	CourseManager courseMaintManager = new CourseManager();
	UserManager userMaintManager = new UserManager();
	RoomManager roomMaintManager = new RoomManager();

	@Override
	public List<Group> listGroup() {

		List<Group> groups = groupManager.findAll();
		return groups;
	}

	public List<Course> listCourses() {
		List<Course> courses = courseMaintManager.listCourse();
		return courses;
	}

	@Override
	public Group saveGroup(String name, int maxStudents, int availableSpots,
			Date startDate, Date finalDate, boolean activeGroup, Course course,
			Shift shift) {

		Group group = new Group(0, name, maxStudents, availableSpots,
				startDate, finalDate, activeGroup, course, shift);
		groupManager.save(group);

		return group;

	}

	public Group updateGroup(int groupid, String name, int maxStudents,
			int availableSpots, Date startDate, Date finalDate,
			boolean activeGroup, Course course, Shift shift) {

		Group group = new Group(groupid, name, maxStudents, availableSpots,
				startDate, finalDate, activeGroup, course, shift);
		groupManager.update(group);

		return group;

	}

	public Registration updateRegistration(int registrationId, int status,
			int groupId) {

		Registration registration = new Registration();
		registration.setRegistrationId(registrationId);
		registration.setStatus(status);
		registration.setGroupId(groupId);

		registrationManager.updateRegistration(registration);
		return registration;

	}

	@Override
	public List<Group> findGroup(String name, Shift shift, Boolean actiu) {

		List<Group> groups = groupManager.find(name, shift, actiu);
		return groups;
	}

	@Override
	public Group getGroup(Integer identificador) {
		Group group = groupManager.findByPrimaryKey(identificador);

		if (group.getAula() != null && group.getAula().getIdAula() != 0) {
			Aula aula = roomMaintManager.findRoomByPrimaryKey(group.getAula()
					.getIdAula());
			group.setAula(aula);
		}
		if (group.getTeacher() != null && group.getTeacher().getNIF() != null) {
			Usuari usuari = userMaintManager.findTeacherById(group.getTeacher()
					.getNIF());
			group.setTeacher(usuari);
		}

		return group;
	}

	public List<Registration> listRegistration() {

		List<Registration> registrations;
		try {
			registrations = registrationManager.findAllRegistrations();
		} catch (Exception e) {
			e.printStackTrace();
			registrations = new ArrayList<Registration>();
		}
		return registrations;
	}

	@Override
	public Registration getRegistration(Integer identificador) {
		Registration registration;
		try {
			registration = registrationManager
					.findRegByPrimaryKey(identificador);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("reg not exist or error");
		}
		return registration;
	}

	@Override
	public Course findCourseByName(String name) {
		Course courseFilter = new Course();
		courseFilter.setCourseName(name);
		Course course = courseMaintManager.findCourses(courseFilter).get(0);
		return course;
	}

	@Override
	public Group findGroupByNameAndShift(String name, Shift shift) {
		Group group = groupManager.findGroupByNameAndShift(name, shift);
		return group;
	}

	@Override
	public Group updateGroup(int groupId, int availableSpots) {
		Group group = new Group(groupId, availableSpots);
		groupManager.updateSpots(group);

		return group;

	}

	@Override
	public List<Usuari> listTeachers() {
		List<Usuari> teachers = userMaintManager.findAllTeachers();
		return teachers;
	}

	@Override
	public List<Aula> listRooms() {
		List<Aula> aules = roomMaintManager.findAllRooms();
		return aules;
	}

	@Override
	public Aula findRoomByName(String name) {
		Aula aula = roomMaintManager.findRoomByName(name);
		return aula;
	}

	@Override
	public void saveResources(int groupid, Aula aula, Usuari prof) {
		Group group = new Group(groupid, aula, prof);
		groupManager.updateResources(group, aula, prof);
	}

	@Override
	public Attendance saveAttendance(Group group) {
		Attendance attendance = attendanceManager.saveAttendance(group);
		return attendance;

	}

	@Override
	public Usuari findStudent(String userId) {
		Usuari student = userMaintManager.findStudent(userId);

		return student;
	}

	public List<Registration> getAllPendingRegByStudent(String idUsuari)
			throws RemoteException {
		List<Registration> regs = registrationManager
				.getPendingRegByStudent(idUsuari);
		return regs;
	}

	@Override
	public Usuari findTeacherByNames(String firstname, String lastname)
			throws RemoteException {
		Usuari teacher = userMaintManager.findTeacherByNames(firstname,
				lastname);
		return teacher;
	}

	@Override
	public List<Registration> findRegistrations(String nif, int cursid,
			int statusint) throws RemoteException {
		List<Registration> registrations = registrationManager.find(nif,
				cursid, statusint);
		return registrations;
	}

	@Override
	public void closeAttendance(Attendance attendance) {
		attendanceManager.closeAttendance(attendance);

	}

	@Override
	public List<Group> listGroupsByProf(String profId) throws RemoteException {
		List<Group> groups = groupManager.findGroupsByProf(profId);

		return groups;
	}

	@Override
	public List<Group> listGroupsByAula(int idAula) throws RemoteException {
		List<Group> groups = groupManager.findGroupsByRoom(idAula);

		return groups;
	}

	@Override
	public int countPresents(Attendance attendance) throws RemoteException {
		int presents = attendanceManager.countPresents(attendance);
		return presents;
	}



	@Override
	public void updateAttendance(int presents, int absents,
			Attendance attendance) throws RemoteException {
		attendanceManager.updateAttendance(presents, absents, attendance);

	}

	@Override
	public int countTotal(int groupid) throws RemoteException {
		int total = registrationManager.getAcceptedRegByGroup(groupid);
		return total;
	}

}
